﻿namespace SCCM_Client_Injector_2014
{
    using Microsoft.ConfigurationManagement.ManagementProvider;
    using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    internal class CollectionMemberships
    {
        private List<string> CollectionList;
        private WqlConnectionManager connection;

        public CollectionMemberships(WqlConnectionManager WMIconnection)
        {
            this.connection = WMIconnection;
        }

        public int FindResourceID(string Client)
        {
            int integerValue = 0;
            string query = "SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" + Client + "'";
            foreach (IResultObject collections in this.connection.QueryProcessor.ExecuteQuery(query))
            {
                integerValue = collections["ResourceID"].IntegerValue;
                Application.DoEvents();
            }
            return integerValue;
        }

        public void ListMemberships(CheckedListBox checkedListBoxCollectionsMembership, string Client, Label labelMembershipClientName)
        {
            int num = this.FindResourceID(Client);
            if (num != 0)
            {
                string query = "SELECT * FROM SMS_CollectionMember_a where ResourceID ='" + num + "'";
                checkedListBoxCollectionsMembership.Items.Clear();
                labelMembershipClientName.Text = Client;
                IResultObject queryObject = this.connection.QueryProcessor.ExecuteQuery(query);
                this.CollectionList = new List<string>();
                foreach (IResultObject collections in queryObject)
                {
                    string stringNull = "";
                    string stringTable = "SMS_R_System";
                    IResultObject instance = this.connection.GetInstance("SMS_Collection.CollectionID='" + collections["CollectionID"].StringValue + "'");
                    foreach (IResultObject collections2 in instance.GetArrayItems("CollectionRules"))
                    {
                        try
                        {
                            if (collections2["RuleName"].StringValue.Equals(Client) && collections2["ResourceClassName"].StringValue.Equals(stringTable))
                            {
                                stringNull = "true";
                            }
                        }
                        catch
                        {
                        }
                    }
                    if (stringNull == "true")
                    {
                        this.CollectionList.Add(instance["Name"].StringValue);
                    }
                    else
                    {
                        this.CollectionList.Add(instance["Name"].StringValue + " - (Query based membership)");
                    }
                    Application.DoEvents();
                }
                this.CollectionList.Sort();
                foreach (string stringToAdd in this.CollectionList)
                {
                    checkedListBoxCollectionsMembership.Items.Add(stringToAdd);
                }
                this.CollectionList.Clear();
            }
            else
            {
                MessageBox.Show("Client: " + Client + " does not exist in SCCM.");
            }
        }
    }
}

